import java.lang.reflect.ParameterizedType;

public class GenericDAO<Entity> {
	protected ConexionBBDD conexion;
	protected Class<Entity> entity;

	public GenericDAO() {
		try {
			conexion = new ConexionBBDD();
			ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();            
            this.entity = (Class<Entity>) genericSuperclass.getActualTypeArguments()[1];
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void create(Entity ent) throws Exception {
		try {
			conexion.kem.getTransaction().begin();
			conexion.kem.persist(ent);
			conexion.kem.getTransaction().commit();
		} finally {
			conexion.cerrarConexion();
		}
	}

	public Entity query(Object id) {
		Entity ent;
		try {
			conexion.kem.getTransaction().begin();
			ent = conexion.kem.find(entity, id);
			conexion.kem.getTransaction().commit();
		} catch (Exception e) {
			ent = null;
		} finally {
			conexion.cerrarConexion();
		}
		return ent;
	}
}
